<template>
  <a-drawer
    title="添加组织"
    placement="right"
    width="40%"
    :visible="visible"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="hanldleAdd()">添加</a-button>
      <a-button
        type="primary"
        icon="minus"
        style="margin:0 0 8px 8px"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除</a-button
      >
      <a-button type="primary" style="margin-left:8px" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <a-table
      ref="table"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <!-- <template slot="Type" slot-scope="text">
        <enum-name code="BarCodeRuleType" :value="text"></enum-name>
      </template> -->
      <span slot="action" slot-scope="text, record">
        <template>
          <!-- <a @click="handleEdit(record.Id)">编辑</a>
          <a-divider type="vertical" /> -->
          <a v-if="hasPerm('Base_UserDepartment.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <!-- <edit-form ref="editForm" :parentObj="this"></edit-form> -->
    <departmentList ref="DepartmentList" :parentObj="this"></departmentList>
  </a-drawer>
</template>

<script>
import DepartmentList from './DepartmentList.vue'

const columns = [
  // { title: '用户主键', dataIndex: 'UserID' },
  { title: '机构名称', dataIndex: 'DepartmentName' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    DepartmentList
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      visible: false,
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      title: '',
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: [],
      pid: ''
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http.post('/Base_Manage/Base_UserDepartment/GetDataListByUserid?UserId=' + this.pid, {}).then(resJson => {
        this.loading = false
        this.data = resJson.Data
        if (resJson.Total) {
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        }
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.DepartmentList.openDrawer(this.pid, this.data, '添加组织')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, this.data)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_Manage/Base_UserDepartment/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          }),
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })

    },
    openDrawer (id) {
      this.pid = id
      this.visible = true
      this.getDataList()
    },
    onDrawerClose () {
      this.visible = false
    }
  }
}
</script>
